import glob
import os.path
import random
import shutil


def creator(rawimg_paht="dataset/raw-img", split=0.1):
    subfile_paths = glob.glob(os.path.join(rawimg_paht, '*'))

    for subfile_path in subfile_paths:  # 分别创建train和test文件夹及其子文件夹
        train_dir = subfile_path.replace("raw-img", "train")
        test_dir = subfile_path.replace("raw-img", "test")
        if not os.path.exists(train_dir) or not os.path.exists(test_dir):
            os.makedirs(train_dir)
            os.makedirs(test_dir)

    for subfile_path in subfile_paths:  # 遍历子文件夹
        img_paths = glob.glob(os.path.join(subfile_path, '*'))
        for img_path in img_paths:  # 遍历子文件夹下图片
            if random.random() < split:
                dst_path = img_path.replace("raw-img", "test")
                shutil.copy(img_path, dst_path)
            else:
                dst_path = img_path.replace("raw-img", "train")
                shutil.copy(img_path, dst_path)


creator(rawimg_paht=r"D:\WorkSpace\Animals10\dataset\raw-img")
